﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace FSD_2_Config_Editor
{
    public partial class FTPDialog : Form
    {
        public FTPDialog()
        {
            InitializeComponent();
        }

        private void Hdd1radioButton_CheckedChanged(object sender, EventArgs e)
        {
            Form1.DriveSelection = "/hdd1/fsd2data/";
        }


        public bool IsValidIP(string addr)
        {
            //create our match pattern
            string pattern = @"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b";
            //create our Regular Expression object
            Regex check = new Regex(pattern);
            //boolean variable to hold the status
            bool valid = false;
            //check to make sure an ip address was provided
            if (addr == "")
            {
                //no address provided so return false
                valid = false;
            }
            else
            {
                //address provided so use the IsMatch Method
                //of the Regular Expression object
                valid = check.IsMatch(addr, 0);
            }
            //return the results
            return valid;
        }

        private void USB0radioButton_CheckedChanged(object sender, EventArgs e)
        {
            Form1.DriveSelection = "/usb0/fsd2data/";
        }

        private void OnBoardMUradioButton_CheckedChanged(object sender, EventArgs e)
        {
            Form1.DriveSelection = "/onboardmu/fsd2data/";
        }

        private void Connectbutton_Click(object sender, EventArgs e)
        {
            if (!IsValidIP(IPAddresstextBox.Text))
            {
                MessageBox.Show("IP address is not valid. Format should be ***.***.***.***", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                IPAddresstextBox.Focus();
            }
            else
            {
            this.DialogResult = DialogResult.OK;

            Form1.ftpServerIP = IPAddresstextBox.Text;
            Form1.ftpUserID = UsernametextBox.Text;
            Form1.ftpPassword = PasswordtextBox.Text;
            WriteSettings();
                this.Close();
            }
        }

        private void WriteSettings()
        {
            string Settings = "ip address = " + Form1.ftpServerIP + Environment.NewLine + "username = " + Form1.ftpUserID
                + Environment.NewLine + "pass = " + Form1.ftpPassword + Environment.NewLine + "port = " + Form1.Port
                + Environment.NewLine + "default path = " + Form1.DriveSelection;

            File.WriteAllText("settings.ini", Settings);
            
        }

        private void Cancelbutton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void FTPDialog_Load(object sender, EventArgs e)
        {
            IPAddresstextBox.Text = Form1.ftpServerIP;
            UsernametextBox.Text = Form1.ftpUserID;
            PasswordtextBox.Text = Form1.ftpPassword;
            PorttextBox.Text = Form1.Port.ToString();

            if (Form1.DriveSelection == "/onboardmu/fsd2data/")
            {
                OnBoardMUradioButton.Checked = true;
            }
            else if (Form1.DriveSelection == "/usb0/fsd2data/")
            {
                USB0radioButton.Checked = true;
            }
            else if (Form1.DriveSelection == "/hdd1/fsd2data/")
            {
                Hdd1radioButton.Checked = true;
            }
            else
            {
                USBMUButton.Checked = true;
            }
        }

        private void PorttextBox_KeyPress(object sender, KeyPressEventArgs e)
        {

            e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != '\b';
        }

        private void USBMUButton_CheckedChanged(object sender, EventArgs e)
        {
            Form1.DriveSelection = "/usbmu0/fsd2data/";
        }
    }
}
